package com.example.beikeyigou;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.beikeyigou.login.UserBean;
import com.example.beikeyigou.untils.BaseActivity;
import com.example.beikeyigou.untils.SaveUserUtils;
import com.example.beikeyigou.untils.URLUtils;
import com.google.gson.Gson;

import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

public class QuestionActivity extends BaseActivity {

    private EditText answerEt;
    private TextView questionTv;
    private ImageView backIv;
    private Button commitBtn;
    private String answer;
    private UserBean.DataBean dataBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question);
        initView();
        setListener();

    }

    private void setListener() {
        commitBtn.setOnClickListener(onClickListener);
        backIv.setOnClickListener(onClickListener);

    }


    private void initView() {
        answerEt = findViewById(R.id.verify_et_answer);
        commitBtn = findViewById(R.id.verify_commit_btn);
        dataBean = SaveUserUtils.getUserInfo(this);
        questionTv = findViewById(R.id.verify_tv_question);
        backIv = findViewById(R.id.verify_back);


        //修改密码前必须先登录
        if (dataBean == null){
            Toast.makeText(QuestionActivity.this,"请先登录后再来！！",Toast.LENGTH_SHORT).show();
        }
        else {



            RequestParams params = new RequestParams(URLUtils.showQuestion_url+"?id="+String.valueOf(dataBean.getId()));
            x.http().get(params,new CommonCallback<String>() {
                @Override
                public void onSuccess(String result) {
                    UserBean userBean = new Gson().fromJson(result,UserBean.class);
                    questionTv.setText(userBean.getMessage());

                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {

                }

                @Override
                public void onCancelled(CancelledException cex) {

                }

                @Override
                public void onFinished() {

                }


            });


        }


    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (v.getId()){
                case R.id.verify_back:
                    finish();
                    break;

                case R.id.verify_commit_btn:
                    answer = answerEt.getText().toString();

                    if (TextUtils.isEmpty(answer)){
                        Toast.makeText(QuestionActivity.this,"输入内容不能为空！",Toast.LENGTH_SHORT).show();
                    }
                    else {
                        int id = dataBean.getId();

                        Map<String,String> map = new HashMap<>();
                        map.put("id",String.valueOf(id));
                        map.put("answer",answer);
                        postNetWork(URLUtils.verifyAnswer_url,map);

                    }
                    break;

            }

            }
        };

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        UserBean userBean = new Gson().fromJson(result,UserBean.class);
        int code = userBean.getCode();
        switch (code){
            case 200:
                Toast.makeText(QuestionActivity.this,"密保信息验证成功，可更改密码",Toast.LENGTH_SHORT).show();
                //跳转到更改密码的页面
                Intent intent = new Intent(QuestionActivity.this,AnswerActivity.class);
                startActivity(intent);
                break;
            default:
                Toast.makeText(QuestionActivity.this,"密保问题验证失败，请重新验证！",Toast.LENGTH_SHORT).show();
                break;

        }


    }
}